package org.chen.wordwave.tools.user;

import static com.blankj.utilcode.util.ThreadUtils.runOnUiThread;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;

import com.alibaba.fastjson2.JSONObject;
import com.hjq.toast.Toaster;

import org.chen.wordwave.Api.ApiClient;
import org.chen.wordwave.Api.ApiService;
import org.chen.wordwave.R;
import org.chen.wordwave.act.Login;
import org.chen.wordwave.bean.FavoriteWords;
import org.chen.wordwave.bean.Reservation;
import org.chen.wordwave.bean.ResultBody;
import org.chen.wordwave.bean.UserInfo;
import org.chen.wordwave.bean.UserRegisterRequest;
import org.chen.wordwave.bean.Words;
import org.chen.wordwave.init.WordInit;
import org.litepal.LitePal;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserUtils {
    private static ApiService apiService;
    private static UserInfo userInfo = new UserInfo();
    public static UserInfo getCurrentUser(Context context) {

        if (WordInit.userInfo.getEmail() == null){
            UserInfo userInfo = new UserInfo();
            userInfo.setMember(false);
            return userInfo;
        }

        String email = WordInit.userInfo.getEmail();
        userInfo = LitePal.where("email = ?", email).findFirst(UserInfo.class);


        apiService = ApiClient.getClient(context).create(ApiService.class);
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(WordInit.userInfo.getEmail());
        apiService.getUserInfo(userRegisterRequest).enqueue(new retrofit2.Callback<UserInfo>() {
            @Override
            public void onResponse(@NonNull Call<UserInfo> call, @NonNull Response<UserInfo> response) {
                if (response.body() != null){
                    runOnUiThread(() -> {
                        if (userInfo.getBan().equals("true")) {
                            Toaster.show(context.getString(R.string.ban));
                            Intent intent = new Intent(context, Login.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            context.startActivity(intent);
                        }else {
                            userInfo = response.body();
                            updateUserByEmail(userInfo);
                        }
                    });
                }else {
                    WordInit.userInfo = new UserInfo();
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserInfo> call,@NonNull Throwable t) {
                WordInit.userInfo = new UserInfo();
                LitePal.deleteAll(UserInfo.class);
                LitePal.deleteAll(Words.class);
                LitePal.deleteAll(Reservation.class);
                LitePal.deleteAll(FavoriteWords.class);
            }
        });

        return userInfo;
    }

    public static void getCurrentUserNow(Context context) {
        apiService = ApiClient.getClient(context).create(ApiService.class);
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(WordInit.userInfo.getEmail());
        apiService.getUserInfo(userRegisterRequest).enqueue(new retrofit2.Callback<UserInfo>() {
            @Override
            public void onResponse(@NonNull Call<UserInfo> call, @NonNull Response<UserInfo> response) {
                if (response.body() != null){
                    if (userInfo.getBan().equals("true")) {
                        Toaster.show(context.getString(R.string.ban));
                        Intent intent = new Intent(context, Login.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        context.startActivity(intent);
                    }else {
                        userInfo = response.body();
                        updateUserByEmail(userInfo);
                    }

                }else {
                    WordInit.userInfo = new UserInfo();
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserInfo> call,@NonNull Throwable t) {
                WordInit.userInfo = new UserInfo();
                LitePal.deleteAll(UserInfo.class);
                LitePal.deleteAll(Words.class);
                LitePal.deleteAll(Reservation.class);
                LitePal.deleteAll(FavoriteWords.class);
            }
        });

    }
    public static void updateUserByEmail(UserInfo userInfo) {
        UserInfo first = LitePal.where("email = ?", userInfo.getEmail()).findFirst(UserInfo.class);
        if (first != null) {
            userInfo.saveOrUpdate("email = ?",first.getEmail()); // 保存更新
        } else {
            WordInit.userInfo = new UserInfo();
        }
    }

    public static UserInfo getUserInfo(Context context) {

        apiService = ApiClient.getClient(context).create(ApiService.class);
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(WordInit.userInfo.getEmail());
        Call<UserInfo> call = apiService.getUserInfo(userRegisterRequest);
        final UserInfo[] userInfo1 = {new UserInfo()};
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    userInfo1[0] = response.body();
                } else {
                    userInfo1[0] = new UserInfo();
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                userInfo1[0] = new UserInfo();
            }
        });
        return userInfo1[0];
    }
}
